﻿
 var editName = '';
Ext.define('HIT.controller.SetUser', {
    extend: 'Ext.app.Controller',
    
    models : [ 'UserRights' ,'LeftTree'],
	stores : [ 'UserRights','PermissionTree'] ,
    views: ['SetUserGrid','SetUserRightsAddPanel','SetUserRightsEditPanel','SetUserRightsPermissionPanel'],

    onButtonClick: function(button, e, options) {
    	var p;              
        if(button.id=='set-user-add'){
        	Share.AjaxRequest({   //判断用户数量是否到达上限
	    		url : ctx + '/CheckUserNumbers',  //后台已经写好，但不知道业务上是否需要用户数量判断，星期一问陆总。
	    		params:{},
	    		callback : function(json) {
	     			if(json.resultcode == '1') {
	     					p = Share.createPanelByAlias('widget.setuserrightsaddpanel'); 
	     					Share.showPanelAsWindow(p);
	     			}else{
	     				Share.ShowMessage("I0054");
	     			}
	    		},
	    		falseFun : function(json) {
	    			Share.ShowMessage("I0054");        					
	    		}
        	});       
        }else if(button.id=='set-user-edit'){
        	 var grid=Ext.getCmp('tab-set-user');             
             if (grid == undefined) {
     			return;
     		}
             var selModel = grid.getSelectionModel();
            if (selModel.hasSelection()) {
                if (selModel.getCount() != 1) {
                	Share.ShowMessage("I0009","编辑");
                    return;
                }
                var model = selModel.getSelection()[0];
                if (model != undefined) {               	
                    p = Share.createPanelByAlias('widget.setuserrightseditpanel');
                    editName = model.get('loginName');
                    Ext.getCmp('loginName').setValue(model.get('loginName'));
                    Ext.getCmp('userName').setValue(model.get('userName'));
                    Ext.getCmp('email').setValue(model.get('email'));  
                    if(model.get('companyId') == 0){
                    	Ext.getCmp('company').setValue('');
                    }else{
                    	Ext.getCmp('company').select((model.get('companyId')));
                    }
                    Ext.getCmp('userId').setValue(model.get('userId'));                   
                    Ext.getCmp('role').setValue(model.get('roleId')); 
                    Share.showPanelAsWindow(p);
                }
            }else{
            	Share.ShowMessage("I0011","需要进行编辑的行");
				return;
            }
        }        
        else if(button.id=='set-user-repsw'){
        	 var grid=Ext.getCmp('tab-set-user');             
             if (grid == undefined) {
     			return;
     		}
             var selModel = grid.getSelectionModel();
        	if(selModel.hasSelection()){
        		 if (selModel.getCount() != 1) {
        			 Share.ShowMessage("I0009","重置密码");
                     return;
                 }
        	 var model = selModel.getSelection()[0];
        	 var userName = model.get('userName');
        	 var userId = model.get('userId');
        	 var password = "111";
				Share.AjaxRequest({
					url : ctx + '/rePassword',
					showMsg:true,
					params : {userId : userId
						     , password : Ext.MD5(password)
					         },
					callback : function(json) {
						if (json.resultcode == '0') {
							grid.getStore().reload();
							Share.showMsg("I","I0075","I0076"," "+userName+" ");
						} else {Share.ShowMessage("I0007","重置密码");}
					}
				});
             }else{
              Share.ShowMessage("I0011","需要进行重置密码操作的行");
			 return;
    	  }
       }

        else if(button.id=='set-user-freeze'){
        	 var grid=Ext.getCmp('tab-set-user');             
             if (grid == undefined) {
     			return;
     		}
             var selModel = grid.getSelectionModel();
        	   if (selModel.hasSelection()) {
                   if (selModel.getCount() != 1) {
                	   Share.ShowMessage("I0009","冻结操作");
                       return;
                   }
                   var model = selModel.getSelection()[0];
                   if(model.get('state') == '冻结' || model.get('state') == 'Frozen'){
                	   Share.ShowMessage("I0082");
                	   return;
                   }
              	   var userName = model.get('userName');
              	   var userId = model.get('userId');
      				Share.AjaxRequest({
      					url : ctx + '/freeze', 
      					params : {userId : userId},
      					callback : function(json) {
      						if (json.resultcode == '0') {
      							grid.getStore().reload();
      							Share.showMsg("I","I0069","I0070"," "+userName+" ");
      						}else if(json.resultcode == '1'){
      							Share.ShowMessage("E0019");
      							}
      						else if(json.resultcode == '2'){
      							Share.ShowMessage("E0020");
      							}	
      						else {
      							Share.ShowMessage("I0007","冻结用户");
      							}
      					}
      				});
               }else{
            	Share.ShowMessage("I0011","需要操作的行");
   				return;
               }
        	}
        else if(button.id=='set-user-unfreeze'){       
        	 var grid=Ext.getCmp('tab-set-user');             
             if (grid == undefined) {
     			return;
     		}
             var selModel = grid.getSelectionModel();
        	   if (selModel.hasSelection()) {
                   if (selModel.getCount() != 1) {
                	   Share.ShowMessage("I0009","解冻");
                       return;
                   }
                   var model = selModel.getSelection()[0];
                   if(model.get('state') == '正常' || model.get('state') == 'Normal'){
                	   Share.ShowMessage("I0083");
                	   return;
                   }
              	   var userName = model.get('userName');
              	   var userId = model.get('userId');
      				Share.AjaxRequest({
      					url : ctx + '/unfreeze',
      					params : {userId : userId},
      					callback : function(json) {
      						if (json.resultcode == '0') {
      							grid.getStore().reload();
      							Share.showMsg("I","I0069","I0071"," "+userName+" ");
      						} else {Share.ShowMessage("I0007","解冻用户");}
      					},
      					falseFun : function(json) {Share.ShowMessage("I0007","解冻用户");}
      				});
               }else{
            	Share.ShowMessage("I0011","需要操作的行");
   				return;
               }
        }
        else if(button.id=='set-user-datapermission'){
        	 var grid=Ext.getCmp('tab-set-user');            
             if (grid == undefined) {
     			return;
     		}
             var selModel = grid.getSelectionModel();
	     if (selModel.hasSelection()) {
             if (selModel.getCount() != 1) {
            	 Share.ShowMessage("I0009","数据权限");
                 return;
                }
             var model = selModel.getSelection()[0];
             if (model != undefined) {
                 p = Share.createPanelByAlias('widget.setuserrightspermissionpanel');               
                 Ext.getCmp('userRights-permission-username').setValue(model.get('userName')); 
                 Ext.getCmp('userId').setValue(model.get('userId')); 
            	 var userId = model.get('userId'); 
                 var tree = Ext.ComponentMgr.get("userRights-permission-tree");                
                 var store = tree.getStore();               
                 store.on('beforeload', function(store) {
         			Ext.apply(store.proxy.extraParams , {userId : userId});        			
            		});
     			 store.load();
            	 Share.showPanelAsWindow(p);
	   }
        }else{
        	Share.ShowMessage("I0011","需要进行数据权限操作的行");
			return;
        }
    }
  },
  
   save:function(win){
	   var grid=Ext.getCmp('tab-set-user');       
       if (grid == undefined) {
			return;
		}
       
//       var form = win.down('form');
       var form = Ext.getCmp('panel-setuserrightsaddpanel');
       if(form == null){
    	   form = Ext.getCmp('panel-setuserrightseditpanel');
       }
       if(form == null){
    	   form = Ext.getCmp('panel-setuserrightspermissionpanel');
       }
       var basicForm = form.getForm();
       if (!basicForm.isValid()) {
           return;
       }
       var functionId = Ext.getCmp('functionId').value;
       if (functionId == 1){
    	   var userId = Ext.ComponentMgr.get("userId").getValue(),              
    	       permissions = Ext.ComponentMgr.get("userRights-permission-tree").getChecked();   
    	   var permissionIds = [];
    		Ext.each(permissions, function(item) {
				permissionIds.push(item.id.split('-')[1]);
			});   	
	    	Share.AjaxRequest({
	    		url : ctx + '/setPermission',
	    		params:{
	    			  permissionItems : permissionIds.join(",")
	    			, userId          : userId   
	    		       },
	    		callback : function(json) {
	     			if(json.resultcode == '0') {	     				
	     				grid.getStore().load();
	     				win.close();
	     				if(userId!=null&&userId!=''){
	     					    Share.ShowMessage("I0006","数据权限修改");
	                		}else{
	                			Share.ShowMessage("I0006","数据权限添加");
	                		}
	     			 
	     			} else {
	     				if(userId!=null&&userId!=''){
	     					    Share.ShowMessage("I0007","数据权限修改");
	                		}else{
	                			Share.ShowMessage("I0007","数据权限添加");
	                		}
	     				
	     			}
	     		},
	     		falseFun : function(json) {
	    			if(userId!=null&&userId!=''){
	    			    Share.ShowMessage("I0007","数据权限修改");
	           		}else{
	           			Share.ShowMessage("I0007","数据权限添加");
	           		}
					
				}
	    	});	    	
	    }
       else if(functionId == 0){
    	var     	   
    	    loginName = Ext.ComponentMgr.get("loginName").getValue(),
    	    userName = Ext.ComponentMgr.get("userName").getValue(),
    	    email = Ext.ComponentMgr.get("email").getValue(),
            companyId = Ext.ComponentMgr.get("company").getValue(),       
    	    password = Ext.ComponentMgr.get("password").getValue(),    
    	    roles = Ext.ComponentMgr.get("role").getValue();
    	
		  	// 保存用户信息
    	Ext.Ajax.request({
    	      	url : ctx + '/addUserRights',
    	    	params : {
    			   loginName  : loginName
    			  ,userName   : userName
    			  ,email      : email
    			  ,companyId  : companyId
    		      ,roleItems  : roles.join(",")
    		      ,password   :Ext.MD5(password)
    		   },
    	      success : function(response) {
    	    	json = Ext.JSON.decode(response.responseText);
    			if(json.resultcode == '0') {   				 
    				grid.getStore().reload();   
    				win.close();  
    				Share.ShowMessage("I0006","用户权限添加");            		
    			} else if (json.resultcode == '1'){
    				Share.ShowMessage("I0013","用户"); 
    			} else {
    				Share.ShowMessage("I0007","用户权限添加");           		   				
    			}
    			
    		},
    		falseFun : function(json) {
    			Share.ShowMessage("I0007","用户权限添加");          					
			}
    	}); 
      }
       else if(functionId == 2){  
       	var 
       	    userId = Ext.ComponentMgr.get("userId").getValue(),
       	    loginName = Ext.ComponentMgr.get("loginName").getValue(),
       	    userName = Ext.ComponentMgr.get("userName").getValue(),
       	    email = Ext.ComponentMgr.get("email").getValue(),
            companyId = Ext.ComponentMgr.get("company").getValue(),       
       	    password = Ext.ComponentMgr.get("password").getValue(),    
       	    confirmPwd = Ext.ComponentMgr.get("confirmpassword").getValue();
       	    roles = Ext.ComponentMgr.get("role").getValue();     
       	    if(Ext.ComponentMgr.get("password").getValue() != '' && password != null){
       	    	if(password != confirmPwd){
       	    	   Ext.getCmp('confirmpassword').markInvalid(Share.T('两次密码不一致！'));
       	    	   return;
       	    	}
       	    }
       	   if( password == ""){
       		 password = "";
       	   }else{
       		 password = Ext.MD5(Ext.ComponentMgr.get("password").getValue());
       	   }
       	 Ext.Ajax.request({
       	      	url : ctx + '/editUserRights',
       	    	params : {
       			   loginName  : loginName
       			  ,userName   : userName
       			  ,email      : email
       			  ,companyId  : companyId
       		      ,roleItems  : roles.join(",")
       		      ,password   : password
       		      ,userId     : userId
       		      ,editName	  : editName
       		   },
       		 success : function(response) {
     	    	json = Ext.JSON.decode(response.responseText);
       			if(json.resultcode == '0') {   				 
       				grid.getStore().reload();   
       				win.close();  
       				Share.ShowMessage("I0006","用户权限修改");              		              		
       			}else if(json.resultcode == '1'){
       				Share.ShowMessage("I0013","用户"); 
       			}else {     
       				Share.ShowMessage("I0007","用户权限修改");
       			}
       			
       		},
       		falseFun : function(json) {
       			    Share.ShowMessage("I0007","用户权限修改");            		   				
   			}
       	}); 
         }
   },
	   
    init: function(application) {
        this.control({
            "#headpanel button": {
                click: this.onButtonClick
            }
        });
    }

});
